package taskcards;

public class JavaScript {
	String head, content, tail;
	int i = 0;
	public JavaScript() {
		head = "var currentContent = 0;\nvar online = navigator.onLine;\n\nfunction Content(title, contentString, networkContent) {\n\t"+
				"this.title = title;\n\t this.getTitle = function () { return this.title; };\n\t"+
			   "this.content = contentString;\n\tthis.getContent = function () { return this.content; };\n\t"+
				"this.network = networkContent;\n\tthis.getNetwork = function () { return this.network; };\n}\n\n" +
			   "function checkStatus() {\n\treturn online;\n}\n\nvar pageContent = new Array();\n";
		content = "";
		tail = "function display(index){\n\tif(pageContent[index].getNetwork() && !online){\n\t\tif(index > currentContent) display(index+1);\n\t\t"+
			   "else display(index-1);\n\t} else { \n\t\tif(index >= 0 && index < pageContent.length) currentContent = index;\n\t\t" +
				"if (currentContent == 0) document.getElementById(\"prev\").style.display = \"none\";\n\t\t" +
			   "else document.getElementById(\"prev\").style.display = \"block\";\n\t\t" +
			   "if (currentContent == (pageContent.length-1)) document.getElementById(\"next\").style.display = \"none\";\n\t\t" +
			   "else document.getElementById(\"next\").style.display = \"block\";\n\t\t" +
				"document.getElementById(\"main\").innerHTML = pageContent[currentContent].getContent();\n\t\t"+
			   "document.getElementById(\"title\").innerHTML = pageContent[currentContent].getTitle();}\n}\n\n" +
			   "function resize(){\n\tvar main = document.getElementById(\"main\");\n\tvar frame = document.getElementById(\"innerContent\");\n\t"+
				"frame.style.width = \"100%\";\n\tframe.style.height = \"100%\";\n\tframe.style.display = \"block\";\n\t"+
			   "main.style.overflow = \"hidden\";\n}";

	}
	
	public void insertContent(String title, String text){
		
		content += "pageContent["+i+"] = new Content(\"" + title + "\",\"" + text + "\",false);\n";
		i++;
	}
	
	public String toString(){
		return this.head + this.content + this.tail;
	}
}
